/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package io.renren.modules.app.controller;


import cn.hutool.core.bean.BeanUtil;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.form.AppLoginForm;
import io.renren.modules.app.utils.JwtUtils;
import io.renren.modules.app.vo.BsDepartmentAdministratorVo;
import io.renren.modules.bs.entity.BsDepartmentAdministratorEntity;
import io.renren.modules.bs.entity.BsDepartmentEntity;
import io.renren.modules.bs.entity.BsHospitalEntity;
import io.renren.modules.bs.service.BsDepartmentAdministratorService;
import io.renren.modules.bs.service.BsDepartmentService;
import io.renren.modules.bs.service.BsHospitalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * APP登录授权
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/app")
@Api(value = "APP登录接口", tags = "登录接口")
public class AppLoginController {

    @Autowired
    private BsDepartmentAdministratorService departmentAdministratorService;

    @Autowired
    private JwtUtils jwtUtils;

    @Autowired
    private BsDepartmentService departmentService;

    @Autowired
    private BsHospitalService hospitalService;


    /**
     * 登录
     */
    @PostMapping("login")
    @ApiOperation("登录")
    @Login
    public R login(AppLoginForm form) {

        BsDepartmentAdministratorVo departmentAdministratorVo = new BsDepartmentAdministratorVo();
        //表单校验
        ValidatorUtils.validateEntity(form);

        //用户登录
        BsDepartmentAdministratorEntity administratorEntity = departmentAdministratorService.login(form);

        administratorEntity.setSalt(null);
        administratorEntity.setAdministratorPwd(null);

        BeanUtil.copyProperties(administratorEntity, departmentAdministratorVo);

        BsDepartmentEntity departmentEntity = departmentService.getById(departmentAdministratorVo.getDepartmentId());
        if (departmentEntity != null) {
            departmentAdministratorVo.setDepartmentName(departmentEntity.getDepartmentName());
            BsHospitalEntity hospitalEntity = this.hospitalService.getById(departmentEntity.getHospitalId());
            departmentAdministratorVo.setHospitalName(hospitalEntity.getHospitalName());
        }

        //生成token
        String token = jwtUtils.generateToken(administratorEntity.getDepartmentAdministratorId());
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("userInfo", departmentAdministratorVo);
        map.put("expire", jwtUtils.getExpire());

        return R.ok(map);
    }

    @PostMapping("test")
    @ApiOperation("测试")
    public R test(HttpServletRequest request) {

        return R.ok().put(request.getAttribute("userId"));
    }

}
